package com.pipi.sso.web.controller;

import com.alibaba.fastjson.JSON;
import com.pipi.sso.core.enums.CommonStatusEnum;
import com.pipi.sso.core.enums.DormEnums;
import com.pipi.sso.core.enums.DormErrorCodeEnum;
import com.pipi.sso.core.exception.DormServiceException;
import com.pipi.sso.core.model.BizResult;
import com.pipi.sso.core.model.RoleBO;
import com.pipi.sso.core.model.UserBO;
import com.pipi.sso.core.service.UserService;
import com.pipi.sso.dao.support.PageResult;
import com.pipi.sso.web.shiro.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * All rights Reserved, Designed By www.maihaoche.com
 *
 * @author pipi
 * @Package com.zj.demo.web.controller
 * @date: 2019/3/21 7:22 PM
 * @Copyright: 2017-2020 www.maihaoche.com Inc. All rights reserved.
 */
@Controller
@RequestMapping("user")
@Slf4j
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private UserInfoService userInfoService;

    @RequestMapping(value = "queryAllUser", method = RequestMethod.GET)
    @ResponseBody
    public List<UserBO> queryAllUser() {
        return userService.queryAllUser();
    }

    @RequestMapping(value = "myInfo.json")
    public String myInfo(Model model) {
        UserBO currentUser = userInfoService.getCurrentUser();
        model.addAttribute("user", currentUser);
        return "user/myInfo";
    }

    @RequestMapping(value = "updatePassword.json")
    @ResponseBody
    public BizResult<Boolean> updatePassword(@RequestBody UserBO userBO) {
        String password = userBO.getPassword();
        String password1 = userBO.getPassword1();
        String password2 = userBO.getPassword2();
        try {
            if (StringUtils.isEmpty(password)
                    || StringUtils.isEmpty(password1)
                    || StringUtils.isEmpty(password2)) {
                return BizResult.create(DormErrorCodeEnum.PARAM_ERROR.getErrCode(), "密码不能为空");
            }
            if (!Objects.equals(password1, password2)) {
                return BizResult.create(DormErrorCodeEnum.PARAM_ERROR.getErrCode(), "两次密码不一致");
            }
            UserBO user = userService.queryUser(userBO).get(0);
            if (!Objects.equals(password, user.getPassword())) {
                return BizResult.create(DormErrorCodeEnum.PARAM_ERROR.getErrCode(), "原密码错误");
            }
            userBO.setPassword(password1);
            userService.updateUser(userBO);
            return BizResult.create(true);
        } catch (DormServiceException e) {
            log.error("修改密码失败！userId is {}, password is {}, password1 is {}, password2 is {}", userBO.getUserId(), password, password1, password2);
            return BizResult.create(e.getCode(), "修改密码失败");
        }
    }


    @RequestMapping(value = "init.json")
    public String init(Model model) {
        //获取所有角色
        List<RoleBO> allRole = DormEnums.RoleEnum.getAllRole();
        model.addAttribute("roleList", allRole);
        return "user/user";
    }

    @RequestMapping(value = "list.json")
    @ResponseBody
    public BizResult<List<UserBO>> list(UserBO userBO) {
        try {
            PageResult<UserBO> pageResult = userService.queryUserByPaged(userBO);
            return BizResult.create(pageResult.getResult(), pageResult.getPageInfo());
        } catch (DormServiceException e) {
            log.error("查询用户失败！userBO is {}", JSON.toJSONString(userBO));
            return BizResult.create(e.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "updateStatus.json")
    @ResponseBody
    public BizResult<Boolean> updateStatus(@RequestBody UserBO userBO) {
        try {
            userService.updateUser(userBO);
            return BizResult.create(true);
        } catch (DormServiceException e) {
            log.error("修改用户信息失败！collegeBO is{}", JSON.toJSONString(userBO), e);
            return BizResult.create(e.getCode(), e.getMessage());
        }
    }


    @RequestMapping(value = "userAdd.json")
    public String userAdd(Model model) {
        List<RoleBO> allRole = DormEnums.RoleEnum.getAllRole();
        model.addAttribute("roleList", allRole);

        return "user/userAdd";
    }

    @RequestMapping(value = "addUser.json")
    @ResponseBody
    public BizResult<Boolean> addUser(@RequestBody UserBO userBO) {
        try {
            userService.addUser(userBO);
            return BizResult.create(true);
        } catch (DormServiceException e) {
            log.error("添加用户失败, userBO is {}", JSON.toJSONString(userBO), e);
            return BizResult.create(e.getCode(), e.getMessage());
        }
    }


    @RequestMapping(value = "userEdit.json")
    public String userEdit(Model model, Long userId) {
        List<RoleBO> allRole = DormEnums.RoleEnum.getAllRole();
        model.addAttribute("roleList", allRole);

        UserBO userBO = new UserBO();
        userBO.setUserId(userId);
        List<UserBO> userBOS = userService.queryUser(userBO);
        model.addAttribute("user", userBOS.get(0));

        return "user/userEdit";
    }

    @RequestMapping(value = "updateUser.json")
    @ResponseBody
    public BizResult<Boolean> updateUser(@RequestBody UserBO userBO) {
        try {
            userService.updateUser(userBO);
            return BizResult.create(true);
        } catch (DormServiceException e) {
            log.error("更新用户失败, userBO is {}", JSON.toJSONString(userBO));
            return BizResult.create(e.getCode(), e.getMessage());
        }
    }


    @RequestMapping(value = "deleteUser.json")
    @ResponseBody
    public BizResult<Boolean> deleteUser(Long userId) {
        try {
            UserBO userBO = new UserBO();
            userBO.setUserId(userId);
            userBO.setIsDeleted(CommonStatusEnum.YES.getCode());
            userService.updateUser(userBO);
            return BizResult.create(true);
        } catch (DormServiceException e) {
            log.error("删除用户失败！userId is {}", userId, e);
            return BizResult.create(e.getCode(), e.getMessage());
        }
    }


}
